package com.collect.senegal.zhiScene.zhiCollection.zhiAdmin;

import android.annotation.SuppressLint;
import android.app.Activity;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.collect.senegal.R;
import com.collect.senegal.zhiAdater.zhiAdminGroupDetail.ZHIGroupOrderDetailAdjuster;
import com.collect.senegal.zhiBase.BaseFragment;
import com.collect.senegal.zhiBean.zhiReceive.ZHIRGroupPerformanceMoneyEntity;
import com.collect.senegal.zhiBean.zhiReceive.ZHIRGroupPerformanceOrderEntity;
import com.collect.senegal.zhiBean.zhiReceive.ZHIRGroupUserEntity;
import com.collect.senegal.zhiBean.zhiSend.ZHISGroupUserPerformanceEntity;
import com.collect.senegal.zhiScene.zhiCollection.zhiAdmin.zhiSort.ZHIAdminCompareByRateASC;
import com.collect.senegal.zhiScene.zhiCollection.zhiAdmin.zhiSort.ZHIAdminCompareByRateDES;
import com.collect.senegal.zhiScene.zhiWidget.zhiFilterAdminGroup.ZHIFilterAdminGroupClickListener;
import com.collect.senegal.zhiScene.zhiWidget.zhiFilterAdminGroup.ZHIFilterAdminGroupOrder;

import java.util.ArrayList;
import java.util.Collections;

import butterknife.BindView;

@SuppressLint("NonConstantResourceId")
public class ZHIAdminGroupOrderFragment extends BaseFragment<ZHIAdminPresenter> implements ZHIAdminContract.View {

    @BindView(R.id.drawer_layout)
    DrawerLayout drawer_layout;

    @BindView(R.id.drawer_filter_admin_grup_order)
    ZHIFilterAdminGroupOrder drawer_filter_admin_grup_order;

    @BindView(R.id.srl_swipe_layout)
    SwipeRefreshLayout srl_swipe_layout;

    @BindView(R.id.tv_page_title)
    TextView tv_page_title;

    @BindView(R.id.tv_cumulative_bonus)
    TextView tv_cumulative_bonus;

    @BindView(R.id.rc_detail_list)
    RecyclerView rc_detail_list;

    @BindView(R.id.iv_sort)
    ImageView iv_sort;

    @BindView(R.id.iv_note)
    ImageView iv_note;
    @BindView(R.id.iv_filter)
    ImageView iv_filter;

    private final ZHIGroupOrderDetailAdjuster mZHIGroupOrderDetailAdjuster = new ZHIGroupOrderDetailAdjuster();

    private ArrayList<ZHIRGroupPerformanceOrderEntity> mGroupPerformanceOrderEntityArrayList;

    /**
     * 是否为降序
     */
    private boolean mIsArraySortDesNow = true;

    private ZHIAdminFragmentInterface mZHIAdminFragmentInterface;

    @Override
    protected void initInjector() {
        mFragmentComponent.inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_admin_group_order_v3;
    }

    @Override
    protected void initView(View view) {
        try {
            srl_swipe_layout.setColorSchemeColors(getResources().getColor(R.color.theme_color_0));

            iv_note.setOnClickListener(v -> {
                if (mZHIAdminFragmentInterface != null) {
                    mZHIAdminFragmentInterface.onFinish();
                }
            });

            iv_sort.setOnClickListener(v -> {
                if (mGroupPerformanceOrderEntityArrayList != null) {
                    if(mIsArraySortDesNow){
                        Collections.sort(mGroupPerformanceOrderEntityArrayList, new ZHIAdminCompareByRateASC());
                        mZHIGroupOrderDetailAdjuster.notifyDataSetChanged();
                        mIsArraySortDesNow = false;
                    }else {
                        Collections.sort(mGroupPerformanceOrderEntityArrayList, new ZHIAdminCompareByRateDES());
                        mZHIGroupOrderDetailAdjuster.notifyDataSetChanged();
                        mIsArraySortDesNow = true;
                    }
                }
            });

            drawer_filter_admin_grup_order.setZHIFilterAdminGroupClickListener(new ZHIFilterAdminGroupClickListener() {
                @Override
                public void onConfirmClick() {
                    if (drawer_layout.isDrawerOpen(drawer_filter_admin_grup_order)) {
                        drawer_layout.closeDrawer(drawer_filter_admin_grup_order);
                    }
                    refreshData();
                }

                @Override
                public void onCloseClick() {
                    if (drawer_layout.isDrawerOpen(drawer_filter_admin_grup_order)) {
                        drawer_layout.closeDrawer(drawer_filter_admin_grup_order);
                    }
                }
            });

            iv_filter.setOnClickListener(v -> {
                showFilterGroupOrder();
            });

            initEvent();
            initData(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mZHIAdminFragmentInterface = (ZHIAdminFragmentInterface) activity;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onGetErrorInfo() {
        srl_swipe_layout.setRefreshing(false);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onGetGroupUserPerformanceMoney(ArrayList<ZHIRGroupPerformanceMoneyEntity> zhirGroupPerformanceMoneyEntityArrayList, ZHISGroupUserPerformanceEntity zhisGroupUserPerformanceEntity) {
        srl_swipe_layout.setRefreshing(false);
    }

    @Override
    public void onGetGroupUserPerformanceOrder(ArrayList<ZHIRGroupPerformanceOrderEntity> zhirGroupPerformanceOrderEntityArrayList, ZHISGroupUserPerformanceEntity zhisGroupUserPerformanceEntity) {
        try {
            srl_swipe_layout.setRefreshing(false);

            ///////////////////////
            int totalRepayNum = 0;
            for (int i = 0; i < zhirGroupPerformanceOrderEntityArrayList.size(); i++) {
                ZHIRGroupPerformanceOrderEntity zhirGroupPerformanceOrderEntity = zhirGroupPerformanceOrderEntityArrayList.get(i);
                totalRepayNum += zhirGroupPerformanceOrderEntity.getTodaySettledNumFormat();
            }
            tv_cumulative_bonus.setText(String.valueOf(totalRepayNum));

            ///////////////////////
            Collections.sort(zhirGroupPerformanceOrderEntityArrayList, new ZHIAdminCompareByRateDES());
            mGroupPerformanceOrderEntityArrayList = zhirGroupPerformanceOrderEntityArrayList;

            mZHIGroupOrderDetailAdjuster.setNewData(zhirGroupPerformanceOrderEntityArrayList);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onGetGroupUsers(ArrayList<ZHIRGroupUserEntity> zhirGroupUserEntityArrayList) {
        srl_swipe_layout.setRefreshing(false);
    }

    @SuppressLint({"InflateParams", "SetTextI18n"})
    public void initEvent() {
        srl_swipe_layout.setOnRefreshListener(() -> initData(false));

        rc_detail_list.setLayoutManager(new GridLayoutManager(this.getActivity(), 1));
        rc_detail_list.setAdapter(mZHIGroupOrderDetailAdjuster);
    }

    public void initData(boolean showDialog) {
        ZHISGroupUserPerformanceEntity zhisGroupUserPerformanceEntity = getCaseSearchParams();
        if (zhisGroupUserPerformanceEntity == null) {
            srl_swipe_layout.setRefreshing(false);
            return;
        }
        assert mPresenter != null;
        mPresenter.getGroupUserPerformanceOrder(getActivity(), zhisGroupUserPerformanceEntity, showDialog);
    }

    public void refreshData() {
        initData(true);
    }

    public void showFilterGroupOrder() {
        if (drawer_layout.isDrawerOpen(drawer_filter_admin_grup_order)) {
            drawer_layout.closeDrawer(drawer_filter_admin_grup_order);
        } else {
            drawer_layout.openDrawer(drawer_filter_admin_grup_order);
        }
    }

    public ZHISGroupUserPerformanceEntity getCaseSearchParams() {
        ZHISGroupUserPerformanceEntity zhisGroupUserPerformanceEntity = new ZHISGroupUserPerformanceEntity();
        zhisGroupUserPerformanceEntity.setLeven(drawer_filter_admin_grup_order.getLevel());
        zhisGroupUserPerformanceEntity.setStartDate(drawer_filter_admin_grup_order.getStartDate());
        zhisGroupUserPerformanceEntity.setEndDate(drawer_filter_admin_grup_order.getEndDate());
        zhisGroupUserPerformanceEntity.setSysUserId(drawer_filter_admin_grup_order.getUser());
        return zhisGroupUserPerformanceEntity;
    }
}
